<template>
  <div class="animated fadeInLeft">
    <el-carousel :height="carouselHeight" motion-blur>
      <el-carousel-item v-for="item in adList.top" :key="item.id">
        <a :href="item.targetUrl">
          <el-image :src="item.imgUrl">
            <template #error>
              <div class="image-slot">
                <el-icon><icon-picture /></el-icon>
              </div>
            </template>
          </el-image>
        </a>
      </el-carousel-item>
    </el-carousel>

    <div
      style="margin-top: 20px; width: 100%; height: calc(100vh - 500px)"
      v-infinite-scroll="getDataList"
      :infinite-scroll-disabled="disabled"
      v-if="mobile === 'false' ? true : false"
      v-loading="loading"
      element-loading-text="加载中..."
      element-loading-background="#f2f0f4"
    >
      <div class="video-group">
        <div v-for="item of dataList" :key="item.id">
          <homeVideoItem :data="item"></homeVideoItem>
        </div>
      </div>
      <div v-if="loading" class="fcolor" style="text-align: center">
        <data-load-vue></data-load-vue>
      </div>
      <div
        v-if="noMore"
        class="fcolor"
        style="text-align: center; line-height: 80px"
      >
        No more
      </div>
    </div>
    <div
      style="margin-top: 20px; min-height: 30vh"
      v-loading="loading"
      element-loading-text="加载中..."
      element-loading-background="#f2f0f4"
      v-else
    >
      <div class="video-pc">
        <div v-for="item of dataList" :key="item.id">
          <homeVideoPc :data="item"></homeVideoPc>
        </div>
      </div>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @change="onChange"
        :page-size="20"
        :page-count="Math.ceil(total / 20)"
        hide-on-single-page
        :pager-count="5"
        style="margin-top: 20px"
      />
    </div>
  </div>
  <el-empty description="description" v-if="dataList.length <= 0 && !loading" />
</template>

<script setup>
import { computed, ref, onMounted } from "vue";
import homeVideoItem from "@/components/video/home-video-item.vue";
import homeVideoPc from "@/components/video/home-video-pc.vue";
import request from "@/assets/js/request";
import { ElMessage } from "element-plus";
import dataLoadVue from "@/components/other/data-load.vue";

const mobile = localStorage.getItem("mobile");
const pageNum = ref(1);
const dataList = ref([]);
const adList = ref({});
const total = ref(0);

const carouselHeight = computed(() => {
  return mobile === "false" ? "200px" : "500px";
});
const loading = ref(false);
const noMore = computed(() => dataList.value.length >= 200);
const disabled = computed(() => loading.value || noMore.value);
const getDataList = () => {
  loading.value = true;
  setTimeout(() => {
    request
      .get(`/client/video/list?pageNum=${pageNum.value}`)
      .then((res) => {
        if (res.code === 200) {
          if (mobile === "false") {
            dataList.value.push(...res.data.list);
          } else {
            dataList.value = res.data.list;
          }

          total.value = res.data.total;

          pageNum.value += 1;
          loading.value = false;
        }
      })
      .catch((err) => {
        ElMessage.error(err.message);
      });
  }, 1000);
};

const getAdList = () => {
  request
    .get("/client/ad/home-ad")
    .then((res) => {
      if (res.code === 200) {
        adList.value = res.data;
      }
    })
    .catch((err) => {
      ElMessage.error(err.message);
    });
};

const onChange = (num) => {
  pageNum.value = num;
  getDataList();
};

onMounted(() => {
  getDataList();
  getAdList();
});
</script>

<style scoped lang="scss">
@mixin group-video($group, $gapWidth) {
  display: grid;
  grid-template-columns: repeat($group, 1fr);
  grid-gap: $gapWidth;
  align-items: center;
  justify-content: center;
}

.video-group {
  @include group-video(2, 10px);
}

.demonstration {
  color: var(--el-text-color-secondary);
}

.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.video-pc {
  @include group-video(5, 20px);
}
</style>
